package com.frxuexi.report.view

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.report.ReportIntent
import com.frxuexi.report.ReportViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.util.Date

@Composable
fun CommitReportPage(navHostController: NavHostController,json:String,vm:ReportViewModel= hiltViewModel()) {
    val entity = Gson().fromJson(json, DialogEntity::class.java)
    var now by remember {
        mutableStateOf("")
    }

    var next by remember {
        mutableStateOf("")
    }

    var isShow by remember {
        mutableStateOf(entity.type==1 || entity.type==2 ||entity.type==3)
    }
    val nowtitle=if(entity.type==1) "今日" else if(entity.type==2) "本周" else "本月"
    val nexttitle=if(entity.type==1) "明日" else if(entity.type==2) "下周" else "下月"

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is IUiState.onSuccess<*>->{
                        ToastUtils.showLong("提交成功")
                        navHostController.popBackStack()
                    }
                    is IUiState.onError->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }
    })
    
    Column {
        MyTitleBar(navHostController = navHostController, title = "工作汇报")
        Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
            if(isShow) {
                Text(text = entity.title)
                Text(text = "${nowtitle}工作总结")
            }

            OutlinedTextField(modifier = Modifier.fillMaxWidth(),value = now, onValueChange = {now=it})
            if(isShow) {
                Text(text = "${nexttitle}工作计划")
            }
            OutlinedTextField(modifier = Modifier.fillMaxWidth(),value = next, onValueChange = {next=it})
            Button(modifier = Modifier.fillMaxWidth(), onClick = {
                val map = mutableMapOf<String, Any>()
                map["reportType"]=entity.type
                map["reportTime"]=System.currentTimeMillis()
                map["reportRemind"]="aa"
                map["reportNowTitle"]="${nowtitle}工作总结"
                map["reportNowValue"]=now
                map["reportNextTitle"]="${nexttitle}工作计划"
                map["reportNextValue"]=next
                vm.sendIntent(ReportIntent.addReport(map))
            }) {
                Text(text = "提交")
            }
        }

    }
}